import request from "../request.js"
import store from '@/store/index.js'
import { TOKEN,baseUrl } from '@/constant/index.js'

const base_url = baseUrl

function getToken(){
	const header = { "content-type": "application/json" }
	const yToken = store.getters.yToken || uni.getStorageSync(TOKEN)
	if(yToken){
		header['Authorization'] = `bearer ${yToken}`
	}
	return header
}


/**
 * 投票
 */
export const vote = async (par) => {
	const header = getToken()
	let { data } = await request({
		url: `${base_url}/scvote/vote/addVote`,
		header,
		method: 'POST',
		data:par,
		encryptionStatus:true
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取用户信息失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取用户信息失败'))
	}
	return data.data
}

/**
 * 用户可投票数
 */
export const voteNumber = async () => {
	const header = getToken()
	let { data } = await request({
		url: `${base_url}/scvote/vote/getUserCanVoteNum`,
		header,
		method: 'GET',
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取用户信息失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取用户信息失败'))
	}
	return data.data
}

/**
 * 用户已投票数
 */
export const haveBeenVoteNumber = async () => {
	const header = getToken()
	let { data } = await request({
		url: `${base_url}/scvote/vote/getUserVoteNum`,
		header,
		method: 'GET',
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取用户信息失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取用户信息失败'))
	}
	return data.data
}
